package smlab.astar
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.utils.clearTimeout;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;
	
	import smlab.utils.DisplayObjectUtil;

	/**
	 * 地图模型
	 * @author tb
	 *
	 */
	public class MapModel extends EventDispatcher
	{
		private var _id:uint;

		//地图宽
		private var _width:int = 960;

		//地图高
		private var _height:int = 560;

		//地图格子大小
		private var _gridSize:uint = 10;

		//地图横向格子数
		private var _gridX:uint;

		//地图纵向格数
		private var _gridY:uint;

		//地图总格数
		private var _gridTotal:uint;

		//地图的数组
		private var _data:Array;

		//内容层
		private var _depthLevel:DisplayObjectContainer;
		//底图
		private var _typeLevel:DisplayObjectContainer;
		//地图顶层
		private var _topLevel:DisplayObjectContainer;
		//地图
		private var _root:DisplayObjectContainer;
		//地型样式
		private var _spaceLevel:Sprite;
		//背景层
		private var _backLevel:Bitmap;
		//控制层
		private var _controlLevel:DisplayObjectContainer;
		//按钮层
		private var _btnLevel:DisplayObjectContainer;
		//动画层
		private var _animatorLevel:DisplayObjectContainer;

		private var _loader:Loader;
		private var _timeoutID:uint;
		
		/**
		 * 可以行走的坐标 
		 */		
		private var _allowData:Array = new Array();

		public function MapModel():void
		{
		}
		
		//--------------------------------------------------------------
		// get set
		//--------------------------------------------------------------
		public function get id():uint
		{
			return _id;
		}
		
		public function set id(value:uint):void
		{
			_id = value;
		}
		
		public function get root():DisplayObjectContainer
		{
			return _root;
		}
		
		public function get topLevel():DisplayObjectContainer
		{
			return _topLevel;
		}
		
		public function get typeLevel():DisplayObjectContainer
		{
			return _typeLevel;
		}
		
		public function get depthLevel():DisplayObjectContainer
		{
			return _depthLevel;
		}

		public function get backLevel():Bitmap
		{
			return _backLevel;
		}

		public function get spaceLevel():DisplayObjectContainer
		{
			return _spaceLevel;
		}

		public function get controlLevel():DisplayObjectContainer
		{
			return _controlLevel;
		}

		public function get btnLevel():DisplayObjectContainer
		{
			return _btnLevel;
		}
		
		public function get animatorLevel():DisplayObjectContainer
		{
			return _animatorLevel;
		}

		public function get width():int
		{
			return _spaceLevel.width;
		}

		public function get height():int
		{
			return _spaceLevel.height;
		}

		public function get gridSize():uint
		{
			return _gridSize;
		}

		public function get gridX():uint
		{
			return _gridX;
		}

		public function get gridY():uint
		{
			return _gridY;
		}

		public function get gridTotal():uint
		{
			return _gridTotal;
		}

		public function get data():Array
		{
			return _data;
		}
		
		public function get allowData():Array
		{
			return _allowData;
		}

		public function removeUser(userID:uint):void
		{
		}

		public function destroy():void
		{
			DisplayObjectUtil.removeAllChild(_root);
			_data = null;
			_backLevel.bitmapData.dispose();
			_backLevel = null;
			_depthLevel = null;
			_typeLevel = null;
			_topLevel = null;
			_root = null;
			_spaceLevel = null;
			_controlLevel = null;
			_btnLevel = null;
		}
		
		/**
		 * 是否为障碍
		 * @param p	坐标
		 * @return
		 */
		public function isBlock(p:Point):Boolean
		{
			var px:int = int(p.x / _gridSize);
			var py:int = int(p.y / _gridSize);
			if (px < 0 || px >= _gridX || py < 0 || py >= _gridY)
			{
				return false;
			}
			return _data[px][py];
		}

		//制作地图
		public function makeMapArray():void
		{
			var offsetSize:uint;
			var i:uint = 0
			var j:uint = 0;
			var p:Point = new Point();

			//var _hitBMD:BitmapData = new BitmapData(_width, _height, true, 0x00000000);
			var _hitBMD:BitmapData = new BitmapData(width, height, true, 0x00000000);
			_hitBMD.draw(_typeLevel);

			_data = new Array(_gridX);
			offsetSize = int(_gridSize / 2);

			for (i = 0; i < _gridX; i++)
			{
				_data[i] = new Array(_gridY);
				for (j = 0; j < _gridY; j++)
				{
					if (_hitBMD.getPixel32(i * _gridSize + offsetSize, j * _gridSize + offsetSize))
					{
						_data[i][j] = false;
					}
					else
					{
						_data[i][j] = true;
						_allowData.push(new Point(i*_gridSize,j*_gridSize));
					}
				}
			}

			p = null;
			_hitBMD.dispose();
			_hitBMD = null;
		}

		/**
		 * 加载地图
		 * @param isShowLoading 是否显示LOADING
		 */
		public function loadMap(url:String):void
		{
			if(!_loader)_loader=new Loader;
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onMapComplete);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onMapError);
			_loader.load(new URLRequest(url));
		}
		
		private function onMapError(e:IOErrorEvent):void
		{	
			trace(e);
		}
		
		private function onMapComplete(e:Event):void
		{	
			var li:LoaderInfo=e.target as LoaderInfo;
			
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onMapComplete);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onMapError);
			
			_root = li.content .root as DisplayObjectContainer;
			_depthLevel = _root["depth_mc"];
			_typeLevel = _root["type_mc"];
			_topLevel = _root["top_mc"];
			_controlLevel = _root["control_mc"];
			_btnLevel = _root["buttonLevel"];
			_animatorLevel = _root["animator_mc"];
			
			//空间层，用于地图交互区域
			var mc:Sprite = _root["rect_mc"];
			if(mc)
			{
				_spaceLevel = mc;
				_spaceLevel.alpha = 0;
			}
			else
			{
				_spaceLevel = creatRect();
			}
			//背景层，全位图
			_backLevel = creatBitmap(_root["bg_mc"]);
			
			_root.addChildAt(_backLevel, 0);
			_root.addChildAt(_spaceLevel, 1);
			
			//禁掉动画层的鼠标事件
			if(_animatorLevel)
			{
				_animatorLevel.mouseEnabled = false;
				_animatorLevel.mouseChildren = false;
			}
			
			_spaceLevel.mouseChildren = false;
			_depthLevel.mouseEnabled = false;
			_controlLevel.mouseEnabled = false;
			_btnLevel.mouseEnabled = false;
			_typeLevel.mouseChildren = false;
			_typeLevel.mouseEnabled = false;
			_topLevel.mouseEnabled = false;

			_gridX = int(width / _gridSize);
			_gridY = int(height / _gridSize);
			_gridTotal = _gridX * _gridY;

			dispatchEvent(new Event(Event.COMPLETE));
			initFindPath();
		}

		/**
		 * 初始化寻路
		 *
		 */
		private function initFindPath():void
		{
			_timeoutID = setTimeout(onMakMap, 200);
		}

		/**
		 * 延时一下生成地图，配置地图
		 * @param event
		 *
		 */
		private function onMakMap():void
		{
			if (_timeoutID != 0)
			{
				clearTimeout(_timeoutID);
			}
			makeMapArray();
			
			dispatchEvent(new Event(Event.INIT));
		}

		private function creatRect():Sprite
		{
			var sp:Sprite = new Sprite();
			sp.graphics.beginFill(0, 0);
			sp.graphics.drawRect(0, 0, _width, _height);
			sp.graphics.endFill();
			return sp;
		}

		private function creatBitmap(dis:DisplayObjectContainer):Bitmap
		{
			var bm:Bitmap = new Bitmap();
			var bd:BitmapData = new BitmapData(width, height)
			bd.draw(dis);
			bm.bitmapData = bd;
			
			if(dis){
				if(dis.parent){
					dis.parent.removeChild(dis);
				}
			}
			
			return bm;
		}
	}
}

